package com.wj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.common.base.PageResult;
import com.hospital.common.dto.message.module.*;
import com.hospital.common.pojo.TMessage;
import com.hospital.common.utils.TakePageNum;
import com.hospital.common.vo.message.module.FindAdminMessageVo;
import com.hospital.common.vo.message.module.FindUserReplayMessageVo;
import com.hospital.common.vo.message.module.GetJumpParamsVo;
import com.wj.mapper.TMessageMapper;
import com.wj.service.ITMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 消息表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-09-28
 */
@Service
public class TMessageServiceImpl extends ServiceImpl<TMessageMapper, TMessage> implements ITMessageService {
    @Autowired
    private TMessageMapper tMessageMapper;

    @Override
    public Integer addAdminMessage(AddAdminMessageDto addAdminMessageDto) {
        return tMessageMapper.addAdminMessage(addAdminMessageDto);
    }

    @Override
    public PageResult findAdminMessage(FindAdminMessageDto findAdminMessageDto) {
        findAdminMessageDto.setPageNum(TakePageNum.getPageNum
                (findAdminMessageDto.getPageNum(), findAdminMessageDto.getPageSize()));
        List<TMessage> adminMessage = tMessageMapper.findAdminMessage(findAdminMessageDto);
        List<FindAdminMessageVo> findAdminMessageVoList = BeanUtil.copyToList
                (adminMessage, FindAdminMessageVo.class);

        PageResult<FindAdminMessageVo> pageResult = new PageResult<>(tMessageMapper.findAdminMessageCount
                (), findAdminMessageVoList);

        return pageResult;
    }

    @Override
    public Long addMessage(AddMessageDto addMessageDto) {
        TMessage tMessage = new TMessage();
        tMessage.setSenderId(addMessageDto.getSenderId());
        tMessage.setReceiverId(addMessageDto.getReceiverId());
        tMessage.setMessageContent(addMessageDto.getMessageContent());
        tMessage.setMetaId(addMessageDto.getMetaId());
        tMessage.setContentId(addMessageDto.getContentId());
        tMessage.setMessageType(addMessageDto.getMessageType());

        tMessageMapper.addMessage(tMessage);
        System.out.println(tMessage.getId());
        return tMessage.getId();
    }

    @Override
    public PageResult findExamineMessage(FindExamineMessageDto findExamineMessageDto) {
        findExamineMessageDto.setPageNum(TakePageNum.getPageNum
                (findExamineMessageDto.getPageNum(), findExamineMessageDto.getPageSize()));
        List<TMessage> tMessageList = tMessageMapper.findExamineMessage(findExamineMessageDto);
        Long total = tMessageMapper.findExamineMessageCount(findExamineMessageDto);
        PageResult<TMessage> pageResult = new PageResult<>(total,tMessageList);
        return pageResult;
    }
    public FindUserReplayMessageVo findOneExamineMessage(Long id){

        TMessage oneExamineMessage = tMessageMapper.findOneExamineMessage(id);
        FindUserReplayMessageVo findUserReplayMessageVo = BeanUtil.copyProperties(oneExamineMessage, FindUserReplayMessageVo.class);
        return findUserReplayMessageVo;
    }

    @Override
    public PageResult findUserReplayMessage(FindUserReplayMessageDto findUserReplayMessageDto) {
        findUserReplayMessageDto.setPageNum(TakePageNum.getPageNum(
                findUserReplayMessageDto.getPageNum(), findUserReplayMessageDto.getPageSize()
        ));
        PageResult<FindUserReplayMessageVo> pageResult = new PageResult<>(tMessageMapper.findUserReplayMessageCount(findUserReplayMessageDto),
                tMessageMapper.findUserReplayMessage(findUserReplayMessageDto)
                );
        return pageResult;
    }
    public FindUserReplayMessageVo findOneUserReplayMessage(Long id){
        return tMessageMapper.findOneUserReplayMessage(id);
    }

    @Override
    public Long getAdminMessageCount() {
        return tMessageMapper.findAdminMessageCount();
    }

    @Override
    public Integer readMessage(Long messageId) {
        return tMessageMapper.readMessage(messageId);
    }

    @Override
    public Integer readAllMessage(Long userId, Integer messageType) {
        return tMessageMapper.readAllMessage(userId,messageType);
    }

    @Override
    public GetJumpParamsVo getJumpParams(Long metaId) {
        return tMessageMapper.getJumpParams(metaId);
    }

    @Override
    public Integer deleteMessage(Long messageIds) {
        tMessageMapper.deleteMessage(messageIds);
        return null;
    }
}
